<?php

namespace App\Service\authority;
use App\Model\AuthRole;
use App\Model\Module;
use App\Model\MenuRole;
use App\Model\User;
use App\Service\CommicService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Redis\Redis;
use App\Model\Menu;
use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
class MenuService extends CommicService
{

    #[Inject]
    private AuthRoleService $authRoleService;

    public static $menuActions = [
        'create'       => '创建',
        'update'       => '更新',
        'delete'       => '删除',
        'export'       => '导出',
        'status'       => '状态',
        'detail'       => '查看',
        'view'         => '详情',
        'tree'         => '树形列表',
        'index'        => '列表',
        'batch'        => '批量',
        'batch-update' => '批量更新',
        'batch-delete' => '批量删除',
        'batch-create' => '批量创建',
        'upload'       => '上传',
        'cache'        => '刷新缓存',
        'sync'         => '同步',
    ];
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
    }
    const STATUS_ENABLE  = 1;
    public function search(Array $params){

        $keyword     = $params['keyword'] ?? '';
        $status      = $params['status'] ?? '';
        $moduleIds   = $params['module_ids'] ?? '';
        $pageSize    = $params['pageSize'] ?? 10;
        $page        = $params['page'] ?? 1;

        $query = Menu::query()->select(['id','module_id','controller','action','status','sort','name','created_at','updated_at']);

        if ($keyword) {
            $query->orWhere([['controller', 'like', '%'.$keyword.'%']]);
        }
        if ($moduleIds) {
            $query->where(['module_id' => $moduleIds]);
        }
        if ($status != '') {
            $query->where(['status' => $status]);
        }

        $models = $query->orderBy('updated_at','desc')->paginate($pageSize,['*'],'page',$page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item){
            $data = $item->toArray();
            if(!empty($item->module)){
                $data['model_name'] = $item->module->label;
            }else{
                $data['model_name'] = '';
            }
            if(!empty($item->control)){
                $data['label'] = $item->control->name;
            }else{
                $data['label'] = '';
            }


            $data['status_name']  =  self::$commonStatus[$item->status];

            $selectData[] =$data;
        }
        $data = $models->toArray();
        $data['data'] = $selectData;
        return $data;
    }


    public function createOrUpdate(Array $postData,bool $is_update = false,Menu $menuInfo = null){

        $name = $postData['name'];
        $status = $postData['status'];
        $controller = $postData['controller'];
        $module_id = $postData['module_id'];
        $action = $postData['action'];
        $actions = $postData['actions'] ?? [];
        $moduleInfo = Module::query()->where('id',$module_id)->first();
        if(empty($moduleInfo)){
            return $this->response(SystemCode::SYSTEM_ERROR, '模块不存在');
        }

        $model = !empty($menuInfo)? $menuInfo : new Menu();
        $model->name = $name;
        $model->controller = $controller;
        $model->module_id = $module_id;
        $model->action = $action;
        $model->status = $status;
        $msg     = $model->action . '创建成功';
        if($model->save()){
            if (!$is_update && !empty($actions)) {
                foreach ($actions as $action) {
                    $menuModel = new Menu();
                    $menuModel->module_id = $module_id;
                    $menuModel->action = $action;
                    $menuModel->controller = $controller;
                    $menuModel->status = $status;
                    $menuModel->name   = self::$menuActions[$action] ?? '';
                    if ($menuModel->save()) {
                        $msg .= $menuModel->action . '创建成功';
                    } else {
                        $msg .= $menuModel->action . '创建失败';
                    }
                }
            }
            return $this->response(SystemCode::SYSTEM_SUCCESS,$msg);
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR,'保存错误');
        }


    }

}